﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChBusiness.FlowOperation;
using ChBusiness.Wsbs;
using WF_Business;

public partial class NewCode_Units_UnitRelation : System.Web.UI.Page
{
    protected string NetoWsPath { get; set; }
    protected string FormId { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        NetoWsPath = "http://" + Request.Url.Host + ExtBusiness.SystemConfig.NewWebDisplay;
        FormId = RequestFlowOperation.Instance.GetFormIdByName("关联保密单位或证明单位");
        if (!IsPostBack)
        {
            //BindGrid();
        }
    }
    [System.Web.Services.WebMethod]
    public static string GetIID(string id, string oraType)
    {
        var strSql = string.Format("select u.iid from ut_unitsrelation u where  u.unitsid='{0}' and u.orgtype='{1}'", id, oraType);
        var iid = SysParams.OAConnection().GetValue(strSql);
        if (string.IsNullOrWhiteSpace(iid))
        {
            var sioTemp = new SerialInstanceOperation();
            iid = sioTemp.GetIIDWithEnterprise("", "", "").ToString();
        }
        return iid;
    }

    private void BindGrid()
    {
        var dtTemp = GetData();
        gv.DataSource = dtTemp;
        gv.RecordCount = dtTemp.Rows.Count;
        gv.DataBind();
    }

    private DataTable GetData()
    {
        DataTable dtTemp;
        var builder = new StringBuilder();
        var isRegister = Request["register"];
        builder.Append(@"select * from view_sys_units  where 1=1 ");
        if (isRegister == "1")
        {
            builder.Append(" and isglzl=1");
            builder.Append(" and isglzmdw=1");
        }
        else
        {
            builder.Append(" and ((isglzl = 0 and isglzmdw = 0) or (isglzl = 0 and isglzmdw = 1) or (isglzl = 1 and isglzmdw = 0))");
        }
        if (txtOraName.Text.Trim() != "")
            builder.AppendFormat(" and unitsname like '%{0}%'", txtOraName.Text.Trim());
        if (txtOraCode.Text.Trim() != "")
            builder.AppendFormat(" and organization like '%{0}%'", txtOraCode.Text.Trim());
        builder.Append(" order by unitsname ");
        SysParams.OAConnection().RunSql(builder.ToString(), out dtTemp);
        return dtTemp;
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_OnLoadData(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var dataKey = gv.DataKeys[e.Row.RowIndex];
            var id = dataKey.Values["id"].ToString();
            var isglzl = dataKey.Values["isglzl"].ToString();
            var isglzmdw = dataKey.Values["isglzmdw"].ToString();
            var linkBtnzlgl = (LinkButton)e.Row.FindControl("linkBtnzlgl");
            linkBtnzlgl.Attributes.Add("onclick", "openDivDlg(\"关联资料管理机构\",\"" + id + "\",\"1\");return false;");
            linkBtnzlgl.Text = isglzl == "0" ? "关联资料管理机构" : "已关联资料管理机构";

            var linkBtnzmdw = (LinkButton)e.Row.FindControl("linkBtnzmdw");
            linkBtnzmdw.Attributes.Add("onclick", "openDivDlg(\"关联证明单位\",\"" + id + "\",\"2\");return false;");
            linkBtnzmdw.Text = isglzmdw == "0" ? "关联证明单位" : "已关联证明单位";
        }
    }
}